package cn.wolfcode.wolf2w.spot.controller.admin;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import cn.wolfcode.wolf2w.common.core.domain.R;
import com.baomidou.mybatisplus.core.metadata.IPage;
import cn.wolfcode.wolf2w.spot.query.SpotQuery;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import cn.wolfcode.wolf2w.common.log.annotation.Log;
import cn.wolfcode.wolf2w.common.log.enums.BusinessType;
import cn.wolfcode.wolf2w.common.security.annotation.RequiresPermissions;
import cn.wolfcode.wolf2w.spot.api.domain.Spot;
import cn.wolfcode.wolf2w.spot.service.ISpotService;
import cn.wolfcode.wolf2w.common.core.web.controller.BaseController;
import cn.wolfcode.wolf2w.common.core.utils.poi.ExcelUtil;


/**
 * 景点列表Controller
 * 
 * @author dalang
 * @date 2023-11-23
 */
@RestController
@RequestMapping("/admin/spots")
public class SpotAdminController extends BaseController
{
    @Autowired
    private ISpotService spotService;

    /**
     * 查询景点列表列表
     */
    @RequiresPermissions("spot:spot:list")
    @GetMapping("/list")
    public R<IPage<Spot>> list(SpotQuery qo) {
        return R.ok(spotService.queryPage(qo));
    }


    /**
     * 导出景点列表列表
     */
    @RequiresPermissions("spot:spot:export")
    @Log(title = "景点列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Spot spot)
    {
        List<Spot> list = spotService.list();
        ExcelUtil<Spot> util = new ExcelUtil<Spot>(Spot.class);
        util.exportExcel(response, list, "景点列表数据");
    }

    /**
     * 获取景点列表详细信息
     */
    @RequiresPermissions("spot:spot:query")
    @GetMapping(value = "/{id}")
    public R<Spot> getInfo(@PathVariable("id") Long id)
    {
        return R.ok(spotService.getById(id));
    }

    /**
     * 新增景点列表
     */
    @RequiresPermissions("spot:spot:add")
    @Log(title = "景点列表", businessType = BusinessType.INSERT)
    @PostMapping
    public R<?> add(@RequestBody Spot spot)
    {
        return toAjax(spotService.save(spot));
    }

    /**
     * 修改景点列表
     */
    @RequiresPermissions("spot:spot:edit")
    @Log(title = "景点列表", businessType = BusinessType.UPDATE)
    @PutMapping
    public R<?> edit(@RequestBody Spot spot)
    {
        return toAjax(spotService.updateById(spot));
    }

    /**
     * 删除景点列表
     */
    @RequiresPermissions("spot:spot:remove")
    @Log(title = "景点列表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public R<?> remove(@PathVariable Long[] ids)
    {
        return toAjax(spotService.removeByIds(Arrays.stream(ids).collect(Collectors.toList())));
    }
}
